package top.ochiamalu.aioj.controller;

import cn.dev33.satoken.stp.StpUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.ochiamalu.aioj.common.BaseResponse;
import top.ochiamalu.aioj.common.ErrorCode;
import top.ochiamalu.aioj.domain.dto.favor.FavorDTO;
import top.ochiamalu.aioj.enums.FavorTypeEnum;
import top.ochiamalu.aioj.exception.BusinessException;
import top.ochiamalu.aioj.service.FavorService;
import top.ochiamalu.aioj.util.ResultUtils;

import javax.annotation.Resource;

/**
 * 收藏控制器
 *
 * @author ochiamalu
 * @date 2025/03/08
 */
@RestController
@RequestMapping("/favors")
public class FavorController {

    @Resource
    private FavorService favorService;

    /**
     * 收藏题目
     *
     * @param favorDTO 收藏DTO
     * @return {@link BaseResponse }<{@link Boolean }>
     */
    @PostMapping("/question")
    public BaseResponse<Boolean> favorQuestion(@RequestBody FavorDTO favorDTO) {
        if (favorDTO.getQuestionId() == null || favorDTO.getQuestionId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数错误");
        }
        StpUtil.checkLogin();
        return ResultUtils.success(favorService.favor(favorDTO.getQuestionId(), FavorTypeEnum.QUESTION));
    }

    /**
     * 收藏题集
     *
     * @param favorDTO 收藏DTO
     * @return {@link BaseResponse }<{@link Boolean }>
     */
    @PostMapping("/set")
    public BaseResponse<Boolean> favorSet(@RequestBody FavorDTO favorDTO) {
        if (favorDTO.getSetId() == null || favorDTO.getSetId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数错误");
        }
        StpUtil.checkLogin();
        return ResultUtils.success(favorService.favor(favorDTO.getSetId(), FavorTypeEnum.SET));
    }
}
